package org.jbit.buy.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.jbit.buy.biz.UserBiz;
import org.jbit.buy.bizImpl.UserBizImpl;
import org.jbit.buy.entity.User;



public class register extends HttpServlet {
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			request.setCharacterEncoding("UTF-8");
			response.setContentType("text/html;charset=UTF-8");
			String path = request.getContextPath();
			HttpSession session=request.getSession();
			PrintWriter writer=response.getWriter();
			 ServletContext application = this.getServletContext();//
			 User uu=new User();
				UserBiz ub=new UserBizImpl();
			 String moblie=request.getParameter("phone");
			 String user1=request.getParameter("name");
			 String loginName=request.getParameter("loginName");
			
			  String result="";
			 if(loginName==null){ 
                //进入注册页面
				 System.out.println("进入注册页面");
			 if(moblie==null){
				 System.out.println(11);
				User userExist=ub.findUser(user1);
				 if(userExist==null){
				
					 System.out.println("该用户名可以注册");
					 result="该用户名可以注册";
					 writer.print("[{\"result\":\"" + result +"\"}]");
					 
				 }else{
					 System.out.println("该用户名已经有人使用");
					 result="该用户名已经有人使用";
					 writer.print("[{\"result\":\"" + result +"\"}]");
				 }
				 
			 }else{
				 String user=request.getParameter("registName");
			 String pwd=request.getParameter("pwd");
			 String email=request.getParameter("mail");
			 String phone=request.getParameter("phone");
			
			 uu.setLoginName(user);
			 uu.setPassword(pwd);
			 uu.setEmail(email);
			 uu.setMobile(phone);
			 uu.setSex(1);
			 uu.setType(0);
		     uu.setUserName("zhangheng");
			
			  int line=ub.saveUser(uu);
			
			  if(line>0){
				  result="您好,注册成功";

					writer.print("[{\"result\":\"" + result +"\"}]");
			  }else {
				result="注册失败,请及时联系管理员,以便及时为您处理";
				writer.print("[{\"result\":\"" + result +"\"}]");
			}
			 }}else {
				 //进入登录页面	
				 System.out.println("进入登陆页面");
				 String LoginPwd=request.getParameter("LoginPwd");
				 User userExist=ub.findUser(loginName, LoginPwd);
				 if(userExist==null){
						
					 System.out.println("账号密码不正确");
					 result="账号密码不正确";
					 writer.print("[{\"result\":\"" + result +"\"}]");
					 
				 }else{
					 System.out.println("登录成功");
					 session.setAttribute("loginName",loginName);
					 result="登录成功";
					 writer.print("[{\"result\":\"" + result +"\"}]");
				 }
				 
			}
               writer.flush();
               writer.close();
			
			 
		} catch (Exception e) {
			
			e.printStackTrace();
		}

}
}
